package com.backend.modules.task.controller;

import com.backend.common.model.R;
import com.backend.modules.task.convert.TaskSubmissionConvert;
import com.backend.modules.task.dto.SubmissionQueryDTO;
import com.backend.modules.task.dto.SubmissionReviewDTO;
import com.backend.modules.task.enums.SubmissionStatus;
import com.backend.modules.task.service.TaskSubmissionService;
import com.backend.modules.task.vo.TaskSubmissionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "任务提交", description = "任务提交相关接口")
@RestController
@RequestMapping("/v1/task-submissions")
public class TaskSubmissionController {

    private final TaskSubmissionService taskSubmissionService;

    public TaskSubmissionController(TaskSubmissionService taskSubmissionService) {
        this.taskSubmissionService = taskSubmissionService;
    }

    @Operation(summary = "审核提交")
    @PutMapping("/{id}/review")
    @PreAuthorize("hasRole('TEACHER')")
    public R<Void> review(@PathVariable Long id, @Validated @RequestBody SubmissionReviewDTO dto) {
        taskSubmissionService.review(id, dto);
        return R.ok();
    }

    @Operation(summary = "批量审核提交")
    @PutMapping("/batch/review")
    @PreAuthorize("hasRole('TEACHER')")
    public R<Void> batchReview(@RequestBody List<Long> ids, @Validated @RequestBody SubmissionReviewDTO dto) {
        taskSubmissionService.batchReview(ids, dto);
        return R.ok();
    }

    @Operation(summary = "获取提交详情")
    @GetMapping("/{id}")
    public R<TaskSubmissionVO> getById(@PathVariable Long id) {
        return R.ok(TaskSubmissionConvert.convert(taskSubmissionService.getById(id)));
    }

    @Operation(summary = "分页查询提交记录")
    @GetMapping("/page")
    public R<Page<TaskSubmissionVO>> page(
            @Parameter(description = "任务ID") @RequestParam(required = false) Long taskId,
            @Parameter(description = "学生姓名") @RequestParam(required = false) String studentName,
            @Parameter(description = "学生学号") @RequestParam(required = false) String studentNo,
            @Parameter(description = "提交状态") @RequestParam(required = false) SubmissionStatus status,
            @Parameter(description = "开始时间") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ParameterObject Pageable pageable) {
        
        SubmissionQueryDTO query = new SubmissionQueryDTO();
        query.setTaskId(taskId);
        query.setStudentName(studentName);
        query.setStudentNo(studentNo);
        query.setStatus(status);
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        
        return R.ok(taskSubmissionService.page(query, pageable)
                .map(TaskSubmissionConvert::convert));
    }
} 